import request from './index'

// 用户登录
export function userLogin(data) {
  return request({
    url: '/auth/login',
    method: 'post',
    data: {
      email: data.email,
      password: data.password,
    }
  })
}

// 用户注册
export function userRegister(data) {
  return request({
    url: '/auth/register',
    method: 'post',
    data: {
      email: data.email,
      password: data.password,
      nickname: data.nickname,
    }
  })
}
// 获取当前用户信息
export function getCurrentUser() {
  return request({
    url: '/auth/me',
    method: 'get'
  })
}

// 更新用户信息
export function updateUserProfile(data) {
  return request({
    url: '/users/profile',
    method: 'put',
    data: {
      nickname: data.nickname,
      bio: data.bio,
      gender: data.gender,
      location: data.location,
      birthday: data.birthday,
      website: data.website
    }
  })
}

// 更新用户头像
export function updateUserAvatar(data) {
  const formData = new FormData()
  formData.append('avatar', data.file)
  
  return request({
    url: '/users/avatar',
    method: 'put',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 修改密码
export function changePassword(data) {
  return request({
    url: '/users/password',
    method: 'put',
    data
  })
}

// 获取用户信息
export function getUserInfo(userId) {
  return request({
    url: `/users/${userId}`,
    method: 'get',
    params: {
      withPosts: true,
      withCollections: true
    }
  })
}

// 关注用户
export function followUser(userId) {
  return request({
    url: `/users/${userId}/follow`,
    method: 'post'
  })
}

// 取消关注用户
export function unfollowUser(userId) {
  return request({
    url: `/users/${userId}/unfollow`,
    method: 'delete'
  })
}

// 获取用户统计信息
export function getUserStats(userId) {
  return request({
    url: `/users/${userId}/stats`,
    method: 'get'
  })
}

// 获取用户关注列表
export function getUserFollowings(userId, params = {}) {
  return request({
    url: `/users/${userId}/followings`,
    method: 'get',
    params: {
      page: 1,
      limit: 20,
      ...params
    }
  })
}

// 获取用户粉丝列表
export function getUserFollowers(userId, params = {}) {
  return request({
    url: `/users/${userId}/followers`,
    method: 'get',
    params: {
      page: 1,
      limit: 20,
      ...params
    }
  })
}

// 获取用户收藏的帖子
export function getUserCollections(userId, params = {}) {
  return request({
    url: `/users/${userId}/collections`,
    method: 'get',
    params: {
      page: 1,
      limit: 20,
      sort: 'latest',
      category: params.category,
      ...params
    }
  })
}

// 获取用户笔记本（分类收藏）
export function getUserNotebooks(userId) {
  return request({
    url: `/users/${userId}/notebooks`,
    method: 'get'
  })
}

// 创建笔记本
export function createNotebook(data) {
  return request({
    url: '/users/notebooks',
    method: 'post',
    data: {
      name: data.name,
      description: data.description,
      isPrivate: data.isPrivate
    }
  })
}


// 重置密码
export function resetPassword(data) {
  return request({
    url: '/auth/reset-password',
    method: 'post',
    data
  })
}

// 刷新token
export function refreshToken() {
  return request({
    url: '/auth/refresh',
    method: 'post'
  })
}